function jsonIOSaveError(msg) {
	console.log("jsonIOError tab 5 :: msg:: ", msg);

	$("#dialog-JsonIOSaveErrorSubjects").dialog("open");
	$("#dialog-JsonIOSaveErrorSubjects #cencel").click(function() {
		$("#dialog-JsonIOSaveErrorSubjects").dialog("close");
	});

	makeAllNull();

	showLoader(false);
}

var childSubCoSchoVOData = [{
	"ceGradeId" : "",
	"sectionId" : "",
	"coschId" : "",
	"coschTitle" : "",
	"isMapped" : 0,
	"childs" : ""
}];

var SubjectCoSchoVOData = [{
	"ceGradeId" : "",
	"sectionId" : "",
	"coschId" : "",
	"coschTitle" : "",
	"isMapped" : 0,
	"childs" : ""
}];

function makeAllNull() {
	//clear selected item
	tab4SubjectViewModel.selectedItem(null);
	tab4SubjectViewModel.itemForEditing(null);
};
function myChildCompareFunction(a, b) {
	return a.coschTitle().toLowerCase() > b.coschTitle().toLowerCase() ? 1 : -1;
};

var SubjectCoSchoVO = function(data, mode) {
	var self = this;
	this.ceGradeId = ko.observable();
	this.sectionId = ko.observable();
	this.coschId = ko.observable();
	this.coschTitle = ko.observable();
	this.isOptional = ko.observable();
	this.isMapped = ko.observable();
	//ADDED AFTERWARDS
	this.areaType = ko.observable();
	this.orderNo = ko.observable();

	this.childs = ko.observableArray([]);

	/**
	 * Check the parent if anyone child is checked.
	 */
	self.isMapped.selected = ko.computed(function(item) {

		if (self.areaType() != 1) {
			//not for CoScholastic :: Only for CoCurricular
			//return;
		}

		var isSelected = 0;
		for (var t = 0; t < self.childs().length; t++) {
			var sub = self.childs()[t];
			if (sub.isMapped()) {
				isSelected = 1;
				//only for for CoCurricular

				break;
			}

		}
		if (self.childs().length > 0) {
			self.isMapped(isSelected);
		};

		return isSelected;
	});

	self.isOptional.showMessage = ko.observable();
	/**
	 * Check Uncheck All
	 * @param {Object} item 	: SubjectCoSchoVO
	 * @param {Object} event 	: p.Event
	 */
	self.onParentCheckClick = function(item, event) {
		return;
		if (item.areaType() == 1) {
			//not for CoCurricular :: Only for CoScholastic
			return;
		}
		//console.log("item.isMapped() :: ", itemIsMapped);
		for (var t = 0; t < self.childs().length; t++) {
			var sub = self.childs()[t];
			sub.isMapped(event.target.checked ? 1 : 0);
		}

		function makeAllMapped(isMapped) {

		}

	};
	self.onCheckClick = function(item) {
		console.log("item.isMapped() :: ", item);
		self.onIsOptionalChange();
	};
	self.onIsOptionalChange = function(item) {
		var isMessage = false;
		var count = 0;
		for (var t = 0; t < self.childs().length; t++) {
			var sub = self.childs()[t];
			if (sub.isOptional() && sub.isMapped()) {

				count++;
			}

		}

		if (count == 1) {
			isMessage = true;
		}
		self.isOptional.showMessage(isMessage);
		// console.log("self.isOptional.showMessage :: " , self.isOptional.showMessage());
		return isMessage;
	};
	self.isMapped.subscribe(function(newValue) {
		//self.onIsOptionalChange();

	}, this);

	var mappedData;

	if (data.childs) {
		//childs
		mappedData = jQuery.map(childSubCoSchoVOData, function(item) {
			return new SubjectCoSchoVO(item);
		});
		this.childs(mappedData);

	}

	self.onCoSchoCheckParentRender = function() {
		console.log("onCoSchoCheckParentRender 11 :: ");

	};
	self.onActivateInput = function() {
		console.log("onActivateInput 11 :: ");

	};

	self.cache = function() {
	};
	//populate our model with the initial data
	self.update(data, mode);

};

ko.utils.extend(SubjectCoSchoVO.prototype, {
	update : function(data, mode) {
		self = this;
		//console.log("data.startDate :: " , data.startDate)
		this.ceGradeId(data.ceGradeId || "");
		this.sectionId(data.sectionId || sectionId);
		this.coschId(data.coschId || "");
		this.coschTitle(data.coschTitle || "");
		this.isOptional(data.isOptional || false);
		this.isMapped(data.isMapped || 0);

		this.areaType(data.areaType || 0);
		this.orderNo(data.orderNo || 0);

		var tempMappedData;
		// for new it will be undefined.
		if (data.childs) {
			tempMappedData = jQuery.map(data.childs, function(item) {
				return new SubjectCoSchoVO(item);
			});
			this.childs(tempMappedData);
			this.childs.sort(myCompareFunction);
		}
		//save off the latest data for later use
		this.cache.latestData = data;
	},

	revert : function() {
		this.update(this.cache.latestData, "");
	}
});

// parameter to pass "savedata"

//console.log("urlSaveCoScholastics tab 4:: " + urlSaveCoScholastics);
var isScholasticDataSaved = false;
var isCoScholasticDataSaved = false;

function saveScholasticsDataToServer(saveDataStr, coScho_coCurr_str) {

	var urlSaveScholastics = baseOperationUrl + "saveSecGradeSubjects";
	console.log("saveScholasticsDataToServer urlSaveScholastics:: \n" + urlSaveScholastics);
	console.log("saveDataStr Scholastics:: \n" + saveDataStr);

	var localData = {
		savedata : saveDataStr
	};
	$.ajax({
		url : urlSaveScholastics,
		type : 'POST',
		data : localData,
		dataType : 'json',
		async : false,
		success : function(msg) {
			console.log("saveScholasticsDataToServer Operation copmpleted", msg);

			if ( typeof msg == "object") {
				saveCoScholasticsDataToServer(coScho_coCurr_str);
			} else {
				showLoader(false);
				/**
				 *Show Error Popup
				 */
				$("#dialog-SaveErrorSubjects").dialog("open");
				$("#dialog-SaveErrorSubjects #cencel").click(function() {
					$("#dialog-SaveErrorSubjects").dialog("close");
				});

				makeAllNull();
			}
		},
		error : jsonIOSaveError
	});
}

function saveCoScholasticsDataToServer(saveDataStr) {
	//console.log("saveDataStr Co Scholastics::\n " + saveDataStr);
	//console.log("saveDataStr end Co Scholastics:: ");
	var urlSaveCoScholastics = baseOperationUrl + "saveSecCoScholastics";
	console.log("saveCoScholasticsDataToServer urlSaveCoScholastics:: \n" + urlSaveCoScholastics);
	var localData = {
		savedata : saveDataStr
	};
	$.ajax({
		url : urlSaveCoScholastics,
		type : 'POST',
		data : localData,
		dataType : 'json',
		async : false,
		success : function(msg) {
			console.log("saveCoScholasticsDataToServer Operation copmpleted", msg);
			showLoader(false);
			if ( typeof msg == "object") {
				tab4SubjectViewModel.init();
			} else {
				/**
				 *Show Error Popup
				 */

				$("#dialog-SaveErrorCoSubjects").dialog("open");
				$("#dialog-SaveErrorCoSubjects #cencel").click(function() {
					$("#dialog-SaveErrorCoSubjects").dialog("close");
					tab4SubjectViewModel.init();
				});

			}

			makeAllNull();
		},
		error : jsonIOSaveError
	});
}

ko.bindingHandlers.yourCheckBoxBinding = {
	init : function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {

		//$(element).onchange = Custom.clear;
		// if (!$(element).getAttribute("disabled")) {
		// span.onmousedown = Custom.pushed;
		// span.onmouseup = Custom.check;
		// } else {
		// span.className = span.className += " disabled";
		// }
		// This will be called when the binding is first applied to an element
		// Set up any initial state, event handlers, etc. here
	},
	update : function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
		console.log("update :: ");
		var span;
		var checkboxHeight = 25;
		var radioHeight = 25;
		console.log("init >>>>>>>>>>>>>>>>> :: ");

		span = document.createElement("span");
		span.className = $(element).type;

		if ($(element).checked == true) {
			if ($(element).type == "checkbox") {
				position = "0 -" + (checkboxHeight * 2) + "px";
				span.style.backgroundPosition = position;
			} else {
				position = "0 -" + (radioHeight * 2) + "px";
				span.style.backgroundPosition = position;
			}
		}
		$(element).parentNode.insertBefore(span, $(element));
		// This will be called once when the binding is first applied to an element,
		// and again whenever the associated observable changes value.
		// Update the DOM element based on the supplied values here.
	}
};
function onActivateInput() {
	alert("onActivateInput");
	console.log("onActivateInput 11 funnnnnnnnnnnnnnnnnnnnnnnnnnnnnn:: ");

};